package com.liusand.controller;

import com.liusand.common.Result;
import com.liusand.dto.DishFlavorsDto;
import com.liusand.pojo.PageInfo;
import com.liusand.service.DishService;
import com.liusand.vo.DishFlavorsVo;
import com.liusand.vo.DishVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author :liusha
 * packageName :com.liusand.controller
 * fileName :DishController
 * creationTime :2022/11/23 21:20
 */
@RestController
@RequestMapping("dish")
public class DishController {
    @Autowired
    private DishService dishService;

    @GetMapping("{id}")
    public Result findDishFlavorsVoById(@PathVariable String id){
        if(!StringUtils.hasText(id)){
            return Result.error("查询失败");
        }
        DishFlavorsVo vo = dishService.findDishFlavorsVoById(id);
        if(vo == null){
            return Result.error("查询失败");
        }
        return Result.success(vo);
    }

    @GetMapping("page")
    public Result page(String name,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String type){
        PageInfo<DishVo> pageInfo = dishService.page(name, page, pageSize, type);
        if(pageInfo == null){
            return Result.error("查询失败");
        }
        return Result.success(pageInfo);

    }

    @GetMapping("list")
    public Result list(String categoryId){
        List<DishFlavorsVo> list = dishService.list(categoryId);
        if(list == null){
            return Result.error("查询失败");
        }
        return Result.success(list);
    }

    @PostMapping
    public Result saveDish(@RequestBody DishFlavorsDto dishFlavorsDto){
        boolean re = dishService.saveDish(dishFlavorsDto);
        if(!re){
            return Result.error("保存失败");
        }
        return Result.success();
    }

    @PutMapping
    public Result updateDish(@RequestBody DishFlavorsDto dishFlavorsDto){
        boolean re = dishService.updateDish(dishFlavorsDto);
        if(!re){
            return Result.error("修改失败");
        }
        return Result.success();
    }

    @DeleteMapping
    public Result deleteDish(@RequestParam("ids") String id){
        if(id == null){
            return Result.error("删除失败");
        }
        String[] split = id.split(",");
        List<String> ids = new ArrayList<>(Arrays.asList(split));
        boolean re = dishService.deleteDish(ids);
        if(!re){
            return Result.error("删除失败");
        }
        return Result.success();
    }

    @PostMapping("status/{status}")
    public Result updateStatus(@PathVariable Integer status,@RequestParam("ids") String id){
        if(id == null){
            return Result.error("修改状态失败");
        }
        String[] split = id.split(",");
        List<String> ids = new ArrayList<>(Arrays.asList(split));
        boolean re = dishService.updateStatus(status, ids);
        if(!re){
            return Result.error("修改状态失败");
        }
        return Result.success();
    }
}
